package cn.iurac.testsystem.controller;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.iurac.testsystem.entity.SysNotice;
import cn.iurac.testsystem.entity.SysNotice;
import cn.iurac.testsystem.entity.SysOperation;
import cn.iurac.testsystem.entity.User;
import cn.iurac.testsystem.enums.RoleEnum;
import cn.iurac.testsystem.exception.ServiceException;
import cn.iurac.testsystem.log.OperationLog;
import cn.iurac.testsystem.param.PageRequestParam;
import cn.iurac.testsystem.param.RequestDataParam;
import cn.iurac.testsystem.param.ResponseParam;
import cn.iurac.testsystem.param.request.notice.NoticePageRequestParam;
import cn.iurac.testsystem.param.request.notice.NoticeRequestParam;
import cn.iurac.testsystem.param.request.operation.OperationPageRequestParam;
import cn.iurac.testsystem.service.SysNoticeService;
import cn.iurac.testsystem.service.SysOperationService;
import cn.iurac.testsystem.util.ResponseUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

@RestController
@RequestMapping("/sys")
@Api("系统controller")
public class SysController {

    @Resource
    private SysNoticeService sysNoticeService;
    @Resource
    private SysOperationService sysOperationService;

    @GetMapping("/notice/getLatest")
    @ApiOperation("获取最新的系统公告")
    @OperationLog("获取最新的系统公告")
    public ResponseParam getLatest() {
        SysNotice sysNotice = sysNoticeService.getLatest();
        if (ObjectUtil.isNull(sysNotice)) {
            return ResponseUtil.getErrorResponseParam("找不到系统公告");
        }else {
            return new ResponseParam(sysNotice);
        }
    }

    @RequiresRoles(value = {"teacher","admin","superadmin"},logical = Logical.OR)
    @PostMapping("/notice/listForPage")
    @ApiOperation("公告管理：查询公告")
    @OperationLog("公告管理：查询公告")
    public ResponseParam listForPage(@RequestBody PageRequestParam<NoticePageRequestParam> req) {
        NoticePageRequestParam data = req.getData();
        Page<SysNotice> page = new Page<>(req.getPage(),req.getLimit());
        page = sysNoticeService.listForPage(page,data);
        return new ResponseParam(page);
    }

    @RequiresRoles(value = {"teacher","admin","superadmin"},logical = Logical.OR)
    @PostMapping("/notice/save")
    @ApiOperation("公告管理：添加公告")
    @OperationLog("公告管理：添加公告")
    public ResponseParam save(@Validated @RequestBody RequestDataParam<NoticeRequestParam> req) throws ServiceException {
        NoticeRequestParam data = req.getData();
        ResponseParam responseParam = new ResponseParam();
        boolean status = sysNoticeService.save(data);
        if (!status) {
            responseParam = ResponseUtil.getErrorResponseParam("系统错误!");
        }
        return responseParam;
    }

    @RequiresRoles(value = {"teacher","admin","superadmin"},logical = Logical.OR)
    @PostMapping("/notice/update/{id}")
    @ApiOperation("公告管理：更新公告")
    @OperationLog("公告管理：更新公告")
    public ResponseParam update(@PathVariable("id") Long id, @Validated @RequestBody RequestDataParam<NoticeRequestParam> req) throws ServiceException {
        User user = (User) SecurityUtils.getSubject().getPrincipal();
        SysNotice sysNotice = sysNoticeService.getById(id);
        NoticeRequestParam data = req.getData();
        ResponseParam responseParam = new ResponseParam();
        sysNotice.setTitle(data.getTitle());
        sysNotice.setUpdateTime(DateUtil.date());
        boolean status = sysNoticeService.updateById(sysNotice);
        if (!status) {
            responseParam = ResponseUtil.getErrorResponseParam("系统错误!");
        }
        return responseParam;
    }

    @RequiresRoles(value = {"teacher","admin","superadmin"},logical = Logical.OR)
    @PostMapping("/notice/delete/{id}")
    @ApiOperation("公告管理：删除公告")
    @OperationLog("公告管理：删除公告")
    public ResponseParam delete(@PathVariable("id") Long id) {
        boolean status = sysNoticeService.removeById(id);
        ResponseParam responseParam = new ResponseParam();
        if (!status) {
            responseParam = ResponseUtil.getErrorResponseParam("系统错误!");
        }
        return responseParam;
    }

    @RequiresRoles(value = {"teacher","admin","superadmin"},logical = Logical.OR)
    @PostMapping("/operation/listForPage")
    @ApiOperation("操作日志：查询操作日志")
    public ResponseParam listOperationForPage(@RequestBody PageRequestParam<OperationPageRequestParam> req) {
        OperationPageRequestParam data = req.getData();
        Page<SysOperation> page = new Page<>(req.getPage(),req.getLimit());
        page = sysOperationService.listForPage(page,data);
        return new ResponseParam(page);
    }
}
